package com.jiuqi.abc.model.dao;

import com.jiuqi.abc.model.bean.DriverItemFormula;
import com.jiuqi.bi.util.type.GUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//动因项公式信息DAO
@Repository
public class ABCDriverItemFormulaDao {
    //动因项公式表
    private static final String T_TABLE = "ABC_M_DRIVER_VALUE_FORMULA";


    //动因项ID 与 动因项表 中 表ID(主键)关联
    private static final String F_AMDV_ID = "AMDV_ID";

    //所属模型ID 与 分摊模型基本信息表 中 模型ID（主键） 关联
    private static final String F_AMM_ID = "AMM_ID";

    //动因项取数公式
    private static final String F_AMDV_FORMULA = "AMDV_FORMULA";

    @Autowired
    JdbcTemplate jdbcTemplate;

    //【增】 动因项公式
    public void batchAddDriverItemFormula(List<DriverItemFormula> driverItemFormulas) {
        String addSql = String.format("INSERT INTO %s VALUES(?,?,?,?)", T_TABLE);
        List<Object[]> argList = new ArrayList<>();
        for (DriverItemFormula driverItemFormula : driverItemFormulas) {
            Object[] args = {GUID.newGUID(), driverItemFormula.getBelongDriverItemId(),
                    driverItemFormula.getBelongModelId(), driverItemFormula.getFormula()};
            argList.add(args);
        }
        jdbcTemplate.batchUpdate(addSql, argList);
    }

    //【查】根据 动因项ID集合 批量获取 动因公式集合
    public List<DriverItemFormula> batchGetDriverItemFormulaByDriverItemIdList(List<String> driverItemIdList) {
        //查询SQL语句
        String selectSql = String.format("SELECT %s,%s,%s FROM %s WHERE %s in ( :ids )", F_AMDV_ID, F_AMM_ID, F_AMDV_FORMULA, T_TABLE, F_AMDV_ID);

        // 为空时添加""防止SQL语法错误
        if (driverItemIdList.isEmpty())
            driverItemIdList.add("");

        //配置查询参数
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("ids", driverItemIdList);

        //执行查询
        NamedParameterJdbcTemplate jdbc = new NamedParameterJdbcTemplate(jdbcTemplate);
        List<Map<String, Object>> resultList = jdbc.queryForList(selectSql, paramMap);

        //处理结果
        List<DriverItemFormula> driverItemFormulaList = new ArrayList<>();
        for (Map<String, Object> stringObjectMap : resultList) {
            DriverItemFormula driverItemFormula = new DriverItemFormula();
            driverItemFormula.setBelongDriverItemId((String) stringObjectMap.get(F_AMDV_ID));
            driverItemFormula.setBelongModelId((String) stringObjectMap.get(F_AMM_ID));
            driverItemFormula.setFormula((String) stringObjectMap.get(F_AMDV_FORMULA));

            driverItemFormulaList.add(driverItemFormula);
        }

        return driverItemFormulaList;
    }

    //【删】 根据 动因项ID集合 批量删除 动因项公式
    public void batchDeleteDriverItemFormula(List<String> driverItemIdList) {
        String deleteSql = String.format("DELETE FROM %s WHERE %s = ?", T_TABLE, F_AMDV_ID);
        List<Object[]> argsList = new ArrayList<>();
        for (String driverItemId : driverItemIdList) {
            argsList.add(new Object[]{driverItemId});
        }
        jdbcTemplate.batchUpdate(deleteSql, argsList);
    }
}
